package com.ty.user.starter.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ty.user.starter.entity.criteria.RoleCriteria;
import com.ty.user.starter.entity.dto.RoleDto;
import com.ty.user.starter.entity.vo.RoleVo;
import com.ty.user.starter.service.RoleService;
import com.ty.user.starter.util.R;
import com.ty.user.starter.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author minku
 * @since 2021-10-16
 */
@Api(tags = {"角色管理"})
@RestController
@RequestMapping("/mgt/role")
public class RoleController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation("新增")
    @PostMapping("add")
    public R<String> add(@Valid @RequestBody RoleDto dto) {
        roleService.add(dto);
        return ResponseUtil.success(null);
    }

    @ApiOperation("编辑")
    @PutMapping("edit")
    public R<String> edit(@Valid @RequestBody RoleDto dto) {
        roleService.edit(dto);
        return ResponseUtil.success(null);
    }

    @ApiOperation("删除")
    @PutMapping("{id}")
    public R<String> delete(@PathVariable("id") Long id) {
        roleService.delete(id);
        return ResponseUtil.success(null);
    }

    @ApiOperation("分页")
    @GetMapping("page")
    public R<IPage<RoleVo>> add(@Valid @RequestBody RoleCriteria criteria) {
        return ResponseUtil.success(roleService.findAll(criteria));
    }
}
